<template>
	<template>
		<button type="primary" @click="B">营业执照识别</button>
		<view class="result-item">
			<text>注册号：{{regNum || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>证件名称：{{title || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>法定代表人姓名：{{legalRepresentative || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>企业名称：{{enterpriseName || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>组成形式：{{typeOfEnterprise || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>经营场所/企业住所：{{address || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>公司类型：{{typeOfEnterprise || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text user-select>经营范围：{{businessScope || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>注册资本：{{registeredCapital || '未识别'}}</text>
		</view> 
		<view class="result-item">
			<text>营业期限：{{validPeriod || '未识别'}}</text>
		</view>
		<view class="result-item">
			<text>注册日期/成立日期：{{registeredDate || '未识别'}}</text>
		</view>
	</template>
</template>

<script>
	export default {
		data() {
			return {
				regNum: "",
				title: "",
				legalRepresentative: "",
				enterpriseName: "",
				typeOfEnterprise: "",
				address: "",
				typeOfEnterprise: "",
				businessScope: "",
				registeredCapital: "",
				cardProperty: "",
				validPeriod: "",
				registeredDate: ""
			}
		},
		methods: {
			B() {
				this.photo("B")
			},
			photo(type) {
				let that = this
				wx.chooseMedia({
					count: 1,
					mediaType: ['image', 'video'],
					sourceType: ['album', 'camera'],
					success(res) {
						// tempFilePath可以作为img标签的src属性显示图片
						let imgUrl = res.tempFiles[0].tempFilePath;
						console.log("res.tempFiles[0].tempFilePath" + res.tempFiles[0].tempFilePath)
						console.log("res.tempFiles[0].size)" + res.tempFiles[0].size)
						that.uploadImg(type, imgUrl)
					}
				})
			},
			uploadImg(type, imgUrl) {
				let that = this
				wx.cloud.uploadFile({
					cloudPath: 'ocr/' + type + '.png',
					filePath: imgUrl, // 文件路径
					success: res => {
						console.log("上传成功", res.fileID)
						that.getImgUrl(type, res.fileID)
					},
					fail: err => {
						console.log("上传失败", err)
					}
				})
			},
			getImgUrl(type, imgUrl) {
				let that = this
				wx.cloud.getTempFileURL({
					fileList: [imgUrl],
					success: res => {
						let imgUrl = res.fileList[0].tempFileURL
						console.log("获取图片url成功:", imgUrl + "获取图片type成功:" + type)
						that.shibie(type, imgUrl)
					},
					fail: err => {
						console.log("获取图片url失败", err)
					}
				})
			},
			shibie(type, imgUrl) {
				wx.cloud.callFunction({
					name: "quickstartFunctions",
					data: {
						type: type,
						imgUrl: imgUrl
					},
					success: (res) => {
						this.regNum = res.result.regNum,
						this.title= res.result.title,
						this.legalRepresentative=res.result.legalRepresentative,
						this.enterpriseName=res.result.enterpriseName,
						this.typeOfEnterprise=res.result.typeOfEnterprise,
						this.address=res.result.address,
						this.typeOfEnterprise=res.result.typeOfEnterprise,
						this.businessScope= res.result.businessScope,
						this.registeredCapital = res.result.registeredCapital,
						this.cardProperty= res.result.cardProperty,
						this.validPeriod=res.result.validPeriod,
						this.registeredDate=res.result.registeredDate 
						console.log("识别成功", res)
					},
					fail(res) {
						console.log("识别失败", res)
					}
				})
			}


		}
	}
</script>

<style>
</style>